<template>
  <el-dialog width="1200px" title="计件工资详情" :visible.sync="dialogVisible" @close="handleClose">
    <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
      <el-form-item label="日期" prop="dateRange">
        <el-date-picker
          v-model="page.query.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="班次" prop="shiftId">
        <el-select v-model="page.query.shiftId" clearable style="width: 100%;" placeholder="请选择班次">
          <el-option
            v-for="item in shiftList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="员工" prop="employeeId">
        <employee-query ref="customerQuery" v-model="page.query.employeeId" @choose="handleChooseEmployee" />
      </el-form-item>
      <el-form-item label="工序" prop="processId">
        <el-select
          v-model="page.query.processId"
          style="width: 100%"
          placeholder="请选择工序"
          filterable
          clearable
        >
          <el-option
            v-for="item in processOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
      </el-form-item>
    </el-form>
    <!--      <div>
        <span>分类属性列表</span>
      </div>
      <el-divider />-->
    <div class="table-button">
      <!--        <el-button type="primary" class="el-icon-plus" @click="calculateWagesClick">计算工资
        </el-button>-->
      <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
      <el-tooltip content="刷新" class="table-right-btn">
        <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
      </el-tooltip>
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      style="width: 100%"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45" />
      <el-table-column label="员工名称" prop="employeeName" />
      <el-table-column label="日期" prop="date" />
      <el-table-column label="班次" prop="shiftName" />
      <el-table-column label="产量" prop="quantity" />
      <el-table-column label="工序名称" prop="processName" />
      <el-table-column label="计件工资" prop="pieceRateWage" />
      <el-table-column label="补贴" prop="subsidy" />
      <el-table-column label="合计" prop="total">
        <template slot-scope="scope">
          {{ Number(scope.row.pieceRateWage) + Number(scope.row.subsidy) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateClick(scope.row)"
          >修改
          </el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            placement="top"
            style="margin-left: 10px"
            title="是否删除此条数据"
            @confirm="handleDelConfirm(scope.row)"
          >
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div v-if="updateVisible">
      <update-module ref="updateModule" :visible="updateVisible" @close="handleCloseSubmit" />
    </div>
  </el-dialog>
</template>

<script>
import { selectDetailPage, deletePieceRateWageDetail } from '@/api/financial/pieceRateWage'
import updateModule from '@/views/financial/pieceRateWage/modules/updateModule.vue'
import { selectAllShift } from '@/api/erp/shift'
import { selectAllProcess } from '@/api/erp/process'

export default {
  name: 'PieceRateWageDetail',
  components: { updateModule },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      shiftList: [],
      processOptions: [],
      dialogVisible: this.visible,
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          shiftId: '',
          date: '',
          employeeId: '',
          processId: '',
          dateRange: ['', '']
        }
      },
      updateVisible: false
    }
  },
  mounted() {
    this.doPage()
    this.getProcessList()
    this.selectAllShift()
  },
  methods: {
    init(groupId) {
      this.page.query.groupId = groupId
      this.doPage()
    },
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectDetailPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.employeeName = ''
      this.page.query.employeeId = ''
      this.$refs.customerQuery.resetQueryForm()
      this.doPage()
    },
    // 修改按钮点击
    updateClick(row) {
      this.updateVisible = true
      this.$nextTick(() => {
        this.$refs.updateModule.init(row.id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deletePieceRateWageDetail(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.$emit('close')
    },
    handleCloseSubmit() {
      this.updateVisible = false
      this.handleSearch()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deletePieceRateWageDetail(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    selectAllShift() {
      this.formLoading = true
      selectAllShift().then(resp => {
        if (resp.success) {
          this.shiftList = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    // 获取工序
    getProcessList() {
      selectAllProcess().then(resp => {
        if (resp.success) {
          this.processOptions = resp.data
        }
      })
    },
    handleChooseEmployee(row) {
      if (row) {
        this.page.query.employeeId = row.id
      }
    }
  }
}
</script>

<style scoped>

</style>
